package io;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileDemo7 {

    @Test
    public void test1() {

        /**
         * 建表语句
         */

        String database = "enc_account_database";

        String src = "" +
                "CREATE TABLE IF NOT EXISTS  `dform_template_ins` (\n" +
                "  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',\n" +
                "  `dform_task_id` bigint(20) NOT NULL COMMENT '关联dform_task表',\n" +
                "  `dform_template_id` bigint(20) NOT NULL COMMENT '关联dform_template表',\n" +
                "  `name` varchar(255) NOT NULL COMMENT '模板名称',\n" +
                "  `template_type` varchar(255) NOT NULL COMMENT '示例模板类型，DATA-数据实例,CONF-配置实例',\n" +
                "  `cycle_start_time` datetime DEFAULT NULL COMMENT '周期开始时间',\n" +
                "  `cycle_end_time` datetime DEFAULT NULL COMMENT '周期结束时间',\n" +
                "  `cycle_reminder_time` datetime DEFAULT NULL COMMENT '周期到期催办日期',\n" +
                "  `code` varchar(255) NOT NULL COMMENT '唯一，模板编码,tmp_xxx',\n" +
                "  `head_line_content` varchar(5000) DEFAULT NULL COMMENT '头标题展示存储',\n" +
                "  `remark` varchar(255) DEFAULT NULL COMMENT '备注信息',\n" +
                "  `delete_flag` int(11) DEFAULT '0' COMMENT '删除标记，0-可用，1-已删除',\n" +
                "  `create_time` datetime DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',\n" +
                "  `create_by` varchar(255) DEFAULT NULL COMMENT '创建人，当前操作人登录名称',\n" +
                "  `update_time` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',\n" +
                "  `update_by` varchar(255) DEFAULT NULL COMMENT '系统默认updateBy',\n" +
                "  `dept_code` varchar(50) DEFAULT NULL COMMENT '所属组织，数据权限控制',\n" +
                "  PRIMARY KEY (`id`),\n" +
                "  KEY `idx_dform_task_id` (`dform_task_id`),\n" +
                "  KEY `idx_template_type` (`template_type`)\n" +
                ") ENGINE=InnoDB   DEFAULT CHARSET=utf8mb4 COMMENT='动态表格模板表实例表';"
                +
                "";

        //生成 comment

        /**
         * COMMENT ON TABLE "USER_AUTH"."SYS_AUTHORITY_RESOURCES" IS '权限资源表';
         * COMMENT ON COLUMN "USER_AUTH"."SYS_AUTHORITY_RESOURCES"."PARENT_ID" IS '父菜单ID，一级菜单为0';
         */

        // create table 解析
        String[] split = src.trim().split("\n");


        String tablename = "";


        for (String readline : split) {

            // 提取表名称
            if (readline.contains("CREATE") && readline.contains("TABLE")) {
                if (StringUtils.isBlank(tablename)) {
                    tablename = readline.split("`")[1];
                }
            }
            //有comment 代表是一个新增字段sql 提取 新增字段后面的comment 注释
            String pattern0 = "^.*\\s+COMMENT\\s+.*$";
            Pattern r0 = Pattern.compile(pattern0);
            Matcher m0 = r0.matcher(readline);
            //
            if (m0.find()) {
                Pattern pattern = Pattern.compile("COMMENT\\s*'(.*)'");
                Matcher matcher = pattern.matcher(readline);
                String commment = "";
                if (matcher.find()) {
                    commment = matcher.group(1);
//                    System.out.println(commment);
                }

                Pattern pattern1 = Pattern.compile("`(.*)`");
                Matcher matcher2 = pattern1.matcher(readline);
                String field = "";
                if (matcher2.find()) {
                    field = matcher2.group(1);
//                    System.out.println(field);
                }

                // 通过字段名称 和 表明提取 注释sql
                String format = String.format("COMMENT ON COLUMN %s.%s.%s  IS '%s';", database, tablename, field, commment);
                System.out.println(format);
            }

            //提取 建表语句后面的 comment 注释
            if (readline.contains("COMMENT=") || readline.contains("comment=")) {
                Pattern pattern = Pattern.compile("COMMENT='(.*?)'");
                Matcher matcher = pattern.matcher(readline);
                String commment = "";
                if (matcher.find()) {
                    commment = matcher.group(1);
//                    System.out.println("Table Comment: " + commment);
                }
                String format = String.format("COMMENT ON TABLE %s.%s  IS '%s';", database, tablename, commment);
                System.out.println(format);
            }

        }

    }

}
